package com.example.myanimator.one;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.myanimator.R;


/**
 * Created by chenxuan on 2020/7/10.
 * <p>
 * 自定义带主题的textView 如：xxx：xxx
 */
public class DrawTextViewLearing extends View {

    private float tipSize = 10;
    private int tipColor = Color.parseColor("#ffffff");
    private String tipStr;

    private int contentSize = 10;
    private int contentColor = Color.BLACK;
    private String contentStr;

    private int midPadding;

    private float mDensity;

    private Paint cmPaint;
    private int width;
    private int height;

    public DrawTextViewLearing(Context context) {
        this(context, null);
    }

    public DrawTextViewLearing(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        cmPaint = new Paint();
        if (null != attrs) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TipTextView);

            tipSize = typedArray.getDimensionPixelSize(R.styleable.TipTextView_tipSize, 10);
            tipColor = typedArray.getColor(R.styleable.TipTextView_tipColor, Color.BLACK);
            tipStr = typedArray.getString(R.styleable.TipTextView_tipText);

            contentSize = typedArray.getDimensionPixelSize(R.styleable.TipTextView_contentSize, 10);
            contentColor = typedArray.getColor(R.styleable.TipTextView_contentColor, Color.BLACK);
            contentStr = typedArray.getString(R.styleable.TipTextView_contentText);

            midPadding = typedArray.getDimensionPixelSize(R.styleable.TipTextView_midPadding, 0);


            typedArray.recycle();
        }

        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        mDensity = displayMetrics.density;

        cmPaint.setShader(null);
        cmPaint.setAntiAlias(true);
        cmPaint.setStyle(Paint.Style.FILL);

    }

    Rect tipBounds = new Rect();
    Rect contentBounds = new Rect();

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
//
//        //获取字体大小
//        cmPaint.setColor(tipColor);
//        cmPaint.setTextSize(tipSize);
//        cmPaint.getTextBounds(tipStr, 0, tipStr.length(), tipBounds);
////        width += cmPaint.measureText(tipStr);
//
//
//        cmPaint.setColor(contentColor);
//        cmPaint.setTextSize(contentSize);
//        cmPaint.getTextBounds(contentStr, 0, contentStr.length(), contentBounds);
////        width += cmPaint.measureText(contentStr);
//
//        if (widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
//
//            width = tipBounds.width();
//            width += contentBounds.width();
//            width += getPaddingLeft() + getPaddingRight() + midPadding;
//
//        }
//
//

        /**1、MeasureSpec.UNSPECIFIED -> 未指定尺寸

         2、MeasureSpec.EXACTLA -> 精确尺寸，控件的宽高指定大小或者为FILL_PARENT

         3、MeasureSpec.AT_MOST -> 最大尺寸，控件的宽高为WRAP_CONTENT，
         控件大小一般随着控件的子空间或内容进行变化，此时控件尺寸只要不超过父控件允许的最大尺寸
       */
        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            height = Math.max(tipBounds.height(), contentBounds.height());
            height += getPaddingTop() + getPaddingBottom();
        }
//
//        width = Math.min(width, MeasureSpec.getSize(widthMeasureSpec));
//
        setMeasuredDimension((int) (width + (mDensity * 5)), (int) (height + (mDensity * 5)));
        setMeasuredDimension(width, 100);
    }

    Paint.FontMetricsInt metricsInt;

    @Override
    protected void onDraw(Canvas canvas) {
        //彩色渐变shawdle
        //计算基线


        cmPaint.setColor(tipColor);
        cmPaint.setTextSize(tipSize);

        metricsInt = cmPaint.getFontMetricsInt();


        int dy = (metricsInt.descent - metricsInt.ascent) / 2 - metricsInt.descent;
        int baseLine = getHeight() / 2 + dy;
        int x = getPaddingLeft();

        //白色背景
        canvas.drawColor(Color.parseColor("#ffffff"));

        //文字
        canvas.drawText(tipStr, x, baseLine, cmPaint);
        //橙色矩形框
        cmPaint.setStyle(Paint.Style.STROKE);
        cmPaint.setColor(Color.parseColor("#cc5600"));
        cmPaint.setStrokeWidth(2);
        canvas.drawRect(new RectF(x,1,width,getHeight()),cmPaint);

//        cmPaint.setColor(contentColor);
//        cmPaint.setTextSize(contentSize);
//        metricsInt = cmPaint.getFontMetricsInt();
//
//        dy = (metricsInt.descent - metricsInt.ascent) / 2 - metricsInt.descent;
//        baseLine = getHeight() / 2 + dy;
//
//        x = (int) (getPaddingLeft() + tipBounds.width() + midPadding);
//        CharSequence charSequence = TextUtils.ellipsize(contentStr, cmPaint,
//                getMeasuredWidth() - tipBounds.width() - midPadding
//                , TextUtils.TruncateAt.END);
//        canvas.drawText(charSequence.toString(), x, baseLine, cmPaint);
    }

    public void setTipSize(float tipSize) {
        this.tipSize = tipSize;
        postInvalidate();
    }

    public void setTipColor(int tipColor) {
        this.tipColor = tipColor;
        postInvalidate();
    }

    public void setTipStr(String tipStr) {
        this.tipStr = tipStr;
        postInvalidate();
        requestLayout();
    }

    public void setContentSize(int contentSize) {
        this.contentSize = contentSize;
        postInvalidate();
        requestLayout();
    }

    public void setContentColor(int contentColor) {
        this.contentColor = contentColor;
        postInvalidate();
    }

    public void setContentStr(String contentStr) {
        this.contentStr = contentStr;
        postInvalidate();
        requestLayout();
    }

    public void setMidPadding(int midPadding) {
        this.midPadding = midPadding;
        requestLayout();
    }

    public void setText(String tip, String content) {
        this.tipStr = tip == null ? "" : tip;
        this.contentStr = content == null ? "" : content;
        invalidate();
        requestLayout();
    }
}
